package com.bamboo.tool.util;

import com.bamboo.tool.template.export.entity.ExportDirectory;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bamboo
 * Date 2022/10/11 15:09
 * Description
 */
public class FileGenerationUtil {

    @SneakyThrows
    public static void generation(String templatePath, String templateName, String outputDirectoryPath, List<ExportDirectory> exportDirectors) {
        Configuration configuration = new Configuration(Configuration.getVersion());
        FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(new File(templatePath));
        configuration.setTemplateLoader(fileTemplateLoader);
        configuration.setDefaultEncoding("utf-8");

        Template template = configuration.getTemplate(templateName);
        Writer out = new FileWriter(outputDirectoryPath);
        Map<String, Object> map = new HashMap<>();
        map.put("data", exportDirectors);
        template.process(map, out);
        out.close();
    }


}
